package eu.liquify {
	import eu.liquify.ui.Layout;
	import eu.liquify.ui.TweenNano;
	import eu.liquify.ui.VBox;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	import no.doomsday.console.ConsoleUtil;
	public class LiquifyComps extends Sprite {
		private var items:DisplayObject;
		public var numberValue:Number = 3;
		public var booleanValue:Boolean = true;
		public var point:Point = new Point();
		public function LiquifyComps():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var num:int = 32;
			
			/*
			var xml:XML =
			<comps>
				<panel>
					<vbox>
						<button label="buttonA" />
						<button label="lol" />
						<button label="this is a big button :D" click="test"/>
						<button label="WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" click="test"/>
						<button label="                                    " click="test"/>
						<button label=",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," click="test"/>
						<spinner label="a number" value="15" min="-5" max="30000" />
						<button label="1" />
						<button label="2" />
						<button label="3" />
						<button label="4" />
						<button label="5" />
						<button label="6" />
						<button label="7" />
						<button label="8" />
						<button label="9" />
						<button label="10" />
						<button label="11" />
						<button label="12" />
					</vbox>
				</panel>
			</comps>
			//*/
			
			/*
			var xml:XML =
			<comps>
				<panel>
					<hbox spacing="10" fitToGrid="false">
						<vbox>
							<button label="Load world" click="test" />
							<button label="Load textures" />
							<button label="Cleanup cache" />
							<button label="Unload" />
						</vbox>
						<vbox>
							<spinner label="Width" value="800" />
							<spinner label="Height" value="600" />
							<button label="Aspect ratio lock" toggle="true" click="booleanValue" />
							<button label="Resize" />
						</vbox>
						<vbox gridWidth="140" fixedSize="false">
							<hbox fitToGrid="false">
								<button label="Restart" />
								<button label="Stop" />
							</hbox>
							<spinner label="Memory limit" value="512" />
							<spinner label="View distance" value="2000" />
							<spinner label="Field of view" value="90" />
						</vbox>
						<vbox>
							<hbox fitToGrid="false" fixedSize="false">
								<label label="Profile" />
								<listbox>
									<item label="default" />
									<item label="fast" />
									<item label="quality" />
								</listbox>
							</hbox>
							<button label="Shadows" toggle="true" />
							<spinner label="Strength" change="numberValue;point.x;test" />
							<button label="Strength falloff" toggle="true" />
						</vbox>
					</hbox>
				</panel>
			</comps>
			//*/
			
			
			var panelOffset:Number = 5;
			
			
			var xml:XML =
			<comps>
				<panel x={panelOffset} y={panelOffset} width={stage.stageWidth-panelOffset*2} height={165-panelOffset} padding="6">
					<hbox spacing="10" fitToGrid="false" drawLines="true">
						<hbox spacing="10" fitToGrid="false" drawLines="true">
							<vbox>
								<button label="Load world" />
								<button label="Load textures" />
								<button label="Cleanup cache" />
								<button label="Unload" />
							</vbox>
							<vbox gridWidth="130">
								<hbox autosizeCascade="true" fitToGrid="false">
									<button label="Restart" />
									<button label="Stop" />
								</hbox>
								<listbox label="Profile" >
									<item label="Default" data="default" />
									<item label="Fast" data="fast" />
									<item label="Quality" data="quality" />
								</listbox>
								<label label="Memory usage" padding="0" stretchV="true" fixedSize="true" />
								<spinner label="Memory limit" step="1" restrictStep="true" min="0" max="4095" />
								<progressbar value="0.8" />
								<button label="Save as PNG" />
							</vbox>
							<vbox label="Info" fitToGrid="false">
								<label label="Status" padding="0" />
								<progressbar width="100" value="0.6" />
								<label label="Time info" padding="0" />
								<label label="Pixel speed" padding="0" />
								<label label="Ray speed" padding="0" />
								<progressbar width="100" value="0.35" />
								<label label="Chunk info" padding="0" />
							</vbox>
						</hbox>
						<multitabbox spacing="10" fitToGrid="false" drawLines="true" width="663" height="147">
							<vbox label="Image">
								<spinner value="800" label="Width" step="1" restrictStep="true" min="1" />
								<spinner value="600" label="Height"  step="1" restrictStep="true" min="1" />
								<button label="Aspect ratio lock" toggle="true" selected="true" />
								<button label="Resize" />
							</vbox>
							<vbox label="Camera" gridWidth="130">
								<spinner label="View distance" step="1" restrictStep="true" min="0" />
								<spinner label="Field of view" step="1" min="0" max="180" />
								<button label="Skip first block" toggle="true" />
								<button label="Chunk skipping" toggle="true" selected="true" />
							</vbox>
							<vbox label="Lighting">
								<button label="Shadows" toggle="true" selected="true" />
								<spinner label="Strength" step="0.1" min="0" />
								<button label="Strength falloff" toggle="true" selected="true" />
								<button label="Night mode" toggle="true" />
							</vbox>
							<vbox label="Lighting (cont.)">
								<button label="Ambient occlusion" toggle="true" selected="true" />
								<spinner label="Minimum" step="0.01" />
								<spinner label="Maximum" step="0.01" />
								<button label="Block lighting" toggle="true" />
							</vbox>
							<vbox label="Reflection">
								<button label="Reflections" toggle="true" selected="true" />
								<spinner label="Scatter" step="0.01" />
								<button label="Specular refl." toggle="true" selected="true" />
							</vbox>
							<vbox label="Misc">
								<button label="Transparency" toggle="true" selected="true" />
								<button label="Antialiasing" toggle="true" />
								<spinner label="Samples" step="1" min="1" restrictStep="true" />
							</vbox>
						</multitabbox>
					</hbox>
					<button label="Hide (space)" />
				</panel>
			</comps>
			
			addChild(ConsoleUtil.instance);
			//ConsoleUtil.show();
			ConsoleUtil.dock("bot");
			
			items = Layout.parse(this, xml);
			//(items as UIItem).resize();
			if (items) addChild(items);
			/*
			var hbox:HBox = (items as Panel).getChildAt(0) as HBox;
			for (var i:int = 0; i < hbox.numChildren; i++) {
				var vbox:VBox = hbox.getChildAt(i) as VBox;
				vbox.addEventListener(MouseEvent.ROLL_OVER, rollOver);
				vbox.addEventListener(MouseEvent.ROLL_OUT, rollOut);
			}
			addEventListener(Event.ENTER_FRAME, run);
			//*/
			
			addEventListener(Event.ENTER_FRAME, run);
		}
		
		private function rollOver(e:MouseEvent):void {
			var vbox:VBox = e.target as VBox;
			TweenNano.to(vbox, 0.5, { "gridWidth": 150 } );
		}
		private function rollOut(e:MouseEvent):void {
			var vbox:VBox = e.target as VBox;
			TweenNano.to(vbox, 0.5, { "gridWidth": 100 } );
		}
		
		
		private function run(e:Event):void {
			ConsoleUtil.print(booleanValue+" "+numberValue+" "+point.x);
			
			//UIItem.gridWidth = Math.max(0, int(mouseX/5));
			//UIItem.gridHeight = Math.max(0, mouseY/10);
			/*
			var hbox:HBox = (items as Panel).getChildAt(0) as HBox;
			hbox.gridWidth = Math.max(0, mouseX);
			for (var i:int = 0; i < hbox.numChildren; i++) {
				var vbox:VBox = hbox.getChildAt(i) as VBox;
				//vbox.gridWidth = Math.max(0, int(mouseX));
				vbox.gridWidth = Math.max(0, mouseX);
			}
			*/
			/*
			if (items is UIItem) {
				(items as UIItem).resize();
			}
			//*/
		}
		
		public function test(lol:String = "nfdf"):void {
			ConsoleUtil.print("test");
		}
	}
}